<template>
  <common-list-group class="report-component-wrap">
    <div slot="group" class="group-list-component-wrap">
      <common-tree
        ref="treeRef"
        :is-slot="true"
        :isRequest="isRequest"
        :treeLoading="treeLoading"
        :tree-data="applitionTreeData"
        :defaultExpandAll="false"
        :defaultExpandedKeys="defaultExpandedKeys"
        :tree-prop="{
          label: 'orgName',
        }"
        is-show-icon-for-no-child
        searchForChildren
        search-placeholder="查询分组或者业务系统"
        :current-node-key="currentNodeKey"
        nodeKey="orgId"
        set-node-by-key="isLeaf"
        special-use="API"
        @handleNodeExpand="handleNodeExpand"
        @handleNodeClick="handleTreeNodeClick"
      >
        <div slot-scope="{ scope }" class="tree-item-wrap">
          <common-carousel-text :content="scope.data.orgName" :font-size="14" />
        </div>
      </common-tree>
    </div>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="65px"
      >
        <el-form-item label="接口" prop="url">
          <common-input
            :maxlength="256"
            :verify="false"
            v-model="queryParams.url"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="IP" prop="domain">
          <common-input
            v-model="queryParams.domain"
            clearable
            :verify="false"
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="数据分级" prop="queryLevelId">
          <el-select v-model="queryParams.queryLevelId" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in queryLevelList"
              :key="item.levelId"
              :label="item.name"
              :value="item.levelId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数据分类" prop="queryTypeId">
          <el-cascader
            class="custom-cascader-style"
            style="width: 100%"
            :options="dataTypeList"
            v-model="queryParams.queryTypeId"
            :show-all-levels="false"
            :props="typeDataProps"
            checkStrictly
            clearable
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="识别规则" prop="querySensitiveId">
          <el-select v-model="queryParams.querySensitiveId" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in ruleData"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重要数据" prop="queryTagId">
          <el-cascader
            class="custom-cascader-style"
            style="width: 100%"
            :options="importData"
            v-model="queryParams.queryTagId"
            :show-all-levels="false"
            :props="importDataProps"
            checkStrictly
            clearable
            :append-to-body="false"
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="核心数据" prop="queryCoreId">
          <el-cascader
            class="custom-cascader-style"
            style="width: 100%"
            :options="coreData"
            v-model="queryParams.queryCoreId"
            :show-all-levels="false"
            :props="coreDataProps"
            checkStrictly
            clearable
            :append-to-body="false"
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
      </el-form>
    </common-search>
    <common-table
      slot="content"
      v-loading="loading"
      :data="interfaceList"
      style="width: 100%"
    >
      <el-table-column
        label="应用名称"
        align="left"
        fixed="left"
        prop="applicationValue"
        show-overflow-tooltip
        width="200px"
      />
      <el-table-column
        label="接口"
        align="left"
        prop="url"
        show-overflow-tooltip
        width="280px"
      />
      <el-table-column
        label="IP"
        align="left"
        prop="domain"
        show-overflow-tooltip
        width="160px"
      />
      <el-table-column
        label="识别规则"
        align="left"
        prop="ruleDataName"
        show-overflow-tooltip
        width="280px"
      />
      <el-table-column
        label="数据分级"
        align="left"
        prop="levelDataName"
        show-overflow-tooltip
        width="160px"
      />
      <el-table-column
        label="数据分类"
        align="left"
        prop="dataTypeName"
        show-overflow-tooltip
        width="180px"
      />

      <el-table-column
        label="重要数据"
        align="left"
        prop="tagsDataName"
        show-overflow-tooltip
        width="100px"
      />

      <el-table-column
        label="核心数据"
        align="left"
        prop="coreDataName"
        width="110px"
        show-overflow-tooltip
      />
      <el-table-column label="操作" align="left" width="100px" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="getInterface(scope.row, 2)"
            >详情
          </el-button>
          <el-dropdown @command="moreTool(scope.row, $event)">
            <span
              class="el-dropdown-link"
              style="cursor: pointer; margin-left: 8px; font-size: 12px"
              v-hasPermi="[
                'apisecurity:apiinterface:manualReview',
                'apisecurity:apiinterface:manualTypeReview',
                'apisecurity:apiinterface:manualTagReview',
                'apisecurity:apiinterface:manualCoreReview',
                'apisecurity:apiinterface:manualRuleReview',
              ]"
            >
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" class="size">
              <el-dropdown-item
                command="dataType"
                v-hasPermi="['apisecurity:apiinterface:manualTypeReview']"
              >
                设置数据分类
              </el-dropdown-item>
              <el-dropdown-item
                command="rule"
                v-hasPermi="['apisecurity:apiinterface:manualRuleReview']"
              >
                设置识别规则
              </el-dropdown-item>
              <el-dropdown-item
                command="importData"
                v-hasPermi="['apisecurity:apiinterface:manualTagReview']"
              >
                设置重要数据
              </el-dropdown-item>
              <el-dropdown-item
                command="coreData"
                v-hasPermi="['apisecurity:apiinterface:manualCoreReview']"
              >
                设置核心数据
              </el-dropdown-item>

              <el-dropdown-item command="viewSensitive">
                敏感样例
              </el-dropdown-item>
              <el-dropdown-item
                command="viewDetail"
                v-hasPermi="['apisecurity:apiinterface:query']"
              >
                访问详情
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </common-table>
    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情页 -->
    <common-info-dialog
      :title="detailsTitle"
      :visible.sync="detailsOpen"
      @cancel="cancel"
      :infoLoading="infoLoading"
    >
      <common-info-table>
        <!--        <common-info-table-cell label="资产分组" width="50%">
          {{ matchingFn(form.domainId) }}
        </common-info-table-cell>-->
        <common-info-table-cell label="应用名称" width="50%">
          {{ form.applicationValue }}
        </common-info-table-cell>
        <common-info-table-cell label="业务系统" width="50%">
          {{ form.businessName }}
        </common-info-table-cell>
        <common-info-table-cell label="接口">
          {{ url }}
        </common-info-table-cell>
        <common-info-table-cell label="识别规则">
          {{ form.ruleDataName }}
        </common-info-table-cell>
        <common-info-table-cell label="数据分级">
          {{ form.levelDataName }}
        </common-info-table-cell>
        <common-info-table-cell label="数据分类">
          {{ form.dataTypeName }}
        </common-info-table-cell>
        <common-info-table-cell label="重要数据">
          {{ form.tagsDataName }}
        </common-info-table-cell>
        <common-info-table-cell label="核心数据">
          {{ form.coreDataName }}
        </common-info-table-cell>
        <!-- <common-info-table-cell label="请求敏感字段">
          {{ form.querySensitiveReq }}
        </common-info-table-cell>
        <common-info-table-cell label="当日访问量">
          {{ form.currentVisits }}
        </common-info-table-cell> -->
      </common-info-table>
    </common-info-dialog>

    <!--敏感样例-->
    <sensitive-example ref="sensitiveExample" />
    <!--访问详情-->
    <url-detail v-if="urlDetailShow" @back="goBackUrl" :detailUrl="detailUrl" />

    <!-- 设置重要数据 -->
    <template v-if="showImportData">
      <set-import-data
        ref="impData"
        :info="itemInfo"
        @close="closeDataDialog"
        @refresh="refresh"
      ></set-import-data>
    </template>

    <!-- 设置核心数据 -->
    <template v-if="showCoreData">
      <set-core-data
        ref="coreData"
        :info="itemInfo"
        @close="closeDataDialog"
        @refresh="refresh"
      ></set-core-data>
    </template>

    <!-- 设置识别规则 -->
    <template v-if="showRuleData">
      <set-rule-data
        ref="ruleData"
        :info="itemInfo"
        @close="closeDataDialog"
        @refresh="refresh"
      ></set-rule-data>
    </template>

    <!-- 设置数据分类 -->
    <template v-if="showDataType">
      <set-data-type
        ref="dataType"
        :info="itemInfo"
        @close="closeDataDialog"
        @refresh="refresh"
      ></set-data-type>
    </template>
  </common-list-group>
</template>

<script>
import {
  listInterface,
  getInterface,
  getSensitiveRule,
  listSensitive,
  querySensitiveList,
  queryTagList,
  queryTypeList,
  queryLevelList,
  insert,
} from "@/api/apisecurity/apiinterface/interface";
import BaseList from "@/mixins/BaseList";
import SensitiveExample from "../apiinterface/components/sensitiveExample.vue";
import UrlDetail from "../apiinterface/components/urlDetail.vue";
import { decode, encode } from "@/utils/aesUtils";
import Utils from "@/utils";
import Index from "../apiinterface/mixin/index";
import commonIcon from "@/components/CommonIcon";
import { mapGetters } from "vuex";
import SetDataType from "../apiinterface/components/setDataType.vue";
import SetImportData from "../apiinterface/components/setImportData.vue";
import SetCoreData from "../apiinterface/components/setCoreData.vue";
import SetRuleData from "../apiinterface/components/setRuleData.vue";
import * as cAPI from "@/api/datascan/coredata";
import * as iAPI from "@/api/datascan/importdata";
import { listType } from "@/api/datascan/datatype/type";
import { queryList } from "@/api/datahandler/discernRule";

export default {
  name: "Http",
  mixins: [BaseList, Index],
  components: {
    SensitiveExample,
    UrlDetail,
    commonIcon,
    SetDataType,
    SetImportData,
    SetCoreData,
    SetRuleData,
  },
  data() {
    return {
      showApplicationName: "",
      urlDetailShow: false,
      detailUrl: "",
      infoUpdateLoading: false,
      infoLoading: false,
      typeDataProps: {
        label: "typeName",
        value: "typeId",
        emitPath: false,
      },
      importDataProps: {
        label: "typeName",
        value: "typeId",
        emitPath: false,
      },
      coreDataProps: {
        label: "typeName",
        value: "typeId",
        emitPath: false,
      },
      url: "",
      butOpen: false,

      props: { multiple: true },
      aplManageId: "",
      serverId: "",
      assetGroup: null,
      manualOpen: false,
      listSensitArr: [],
      flowAnalysisDiv: false,
      analysisPath: "",
      analysisId: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selectionArr: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产表格数据
      interfaceList: [],
      // 弹出层标题
      title: "",
      // 资产分组树
      deptOptions: undefined,
      // 资产分组名称
      assetGroupName: undefined,
      assetGroupArr: [],
      // 敏感标识下来
      sensitiveRuleArr: [],
      paramType: [],
      paramValue: [],
      paramTypeObj: {
        longInt: "长整型",
        number: "数值型",
        varchar: "字符型",
        int: "整型",
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      manualId: null,
      // 是否显示弹出层
      open: false,
      batchUpdateOpen: false,
      // 是否显示详情弹出层
      detailsOpen: false,
      batchUpdateTitle: "",
      detailsTitle: "",
      // 新增参数默认一条
      addParamsArr: [1],
      datachildren: [],
      queryTagList: [],
      queryTypeList: [],
      queryLevelList: [],
      // 查询参数
      querySensitiveList: [],
      applicationList: [],
      manualForm: {
        manualInterfaceSensitiveRuleList: null,
        interfaceSensitiveRuleList: null,
        manualInterfaceTagsList: null,
        interfaceDataTagsList: null,
        manualType: null,
        manualTag: null,
      },
      queryParams: {
        IP: null,
        queryCoreId: null,
        querySensitiveId: null,
        queryTagId: null,
        queryTypeId: null,
        deptIds: null,
        pageNum: 1,
        pageSize: 10,
        serverId: [],
        manuCode: null,
        flagSensitive: null,
        systemCode: null,
        path: null,
        schema: null,
        passSource: null,
        domain: null,
        port: null,
        query: null,
        apiName: null,
        domainId: null,
        ruleId: null,
        description: null,
        auth: null,
        method: null,
        fingerprint: null,
        interfaceStatus: null,
        querySensitiveId: null,
        queryTagId: null,
        queryLevelId: null,
        queryTypeId: null,
        applicationValue: null,
        url: null,
      },
      // 表单参数
      form: {
        aplManageId: null,
        applicationName: null,
        businessSystemName: null,
        businessSystemId: null,
        auth: 0,
        url: null,
        applicationValue: "",
        querySensitive: null,
        queryTag: null,
        queryLevel: null,
        querySensitiveReq: null,
        queryType: null,
        currentVisits: 0,
        deptId: null,
        ancestors: null,
        showApplicationName: null,
        passSource: "网关",
      },

      buttonLoading: false,
      options: [],
      distinguishStyle: null,
      parameterFormatData: [
        {
          label: "json",
          value: "application/json",
        },
        {
          label: "x-www-form-urlencoded",
          value: "application/x-www-form-urlencoded",
        },
      ],
      isDisabled: false,
      selectLoading: false,
      queryInfo: {
        applicationName: null,
        pageNum: 1,
        pageSize: 10,
      },
      initDataList: [],
      applitionTreeData: [],
      treeLoading: true,
      currentNodeKey: "",
      isRequest: false,
      // 新添加功能变量
      itemInfo: null,
      showImportData: false,
      showCoreData: false,
      showDataType: false,
      showRuleData: false,
      coreData: [],
      importData: [],
      dataTypeList: [],
      ruleData: [],
    };
  },

  computed: {
    ...mapGetters(["isAdmin", "name"]),
  },

  watch: {
    // 根据名称筛选组织树
    assetGroupName(val) {
      this.$refs.tree.filter(val);
    },
    // applicationValue: {
    //   handler(val) {
    //     this.form.applicationValue = val[1];
    //     this.getIp(val[1]);
    //   },
    //   immediate: true,
    // },
  },
  created() {
    this.getList();
    this.getDataTypeList();
    this.getImportDataList();
    this.getCoreDataList();
    this.getRuleData();
    this.queryLevel();
    this.getApplicationList();

    //解决级联下拉列表偶先卡顿问题
    const $el = document.querySelectorAll(
      ".el-cascader-panel .el-cascader-node[aria-owns]"
    );
    Array.from($el).map((item) => item.removeAttribute("aria-owns"));
  },

  methods: {
    //获取核心数据
    getCoreDataList() {
      cAPI.queryTreeList().then((res) => {
        if (res && res.code === 200) {
          this.coreData = res.data;
        } else {
          this.coreData = [];
        }
      });
    },
    //获取重要数据
    getImportDataList() {
      iAPI.queryTreeList().then((res) => {
        if (res && res.code === 200) {
          this.importData = res.data;
        } else {
          this.importData = [];
        }
      });
    },
    //获取数据分类
    getDataTypeList() {
      listType().then((res) => {
        if (res && res.code === 200) {
          this.dataTypeList = res.data;
        } else {
          this.dataTypeList = [];
        }
      });
    },
    //获取识别规则
    getRuleData() {
      queryList().then((res) => {
        if (res && res.code === 200) {
          this.ruleData = res.rows;
        } else {
          this.ruleData = [];
        }
      });
    },
    refresh() {
      this.closeDataDialog();
      this.getList();
    },
    closeDataDialog() {
      this.showImportData = false;
      this.showCoreData = false;
      this.showDataType = false;
      this.showRuleData = false;
      this.$emit("changeTitle", true);
    },
    saveSystem(params) {
      insert(params).then((res) => {
        if (res && res.code === 200) {
          this.form.businessSystemId = res.data.id;
          this.form.businessSystemName = res.data.businessName;
          this.$refs.systemAdd.buttonLoading = false;
          this.$refs.systemAdd.open = false;

          // console.log(this.form);
        } else {
          this.$GlobalErrorConfirm("编辑失败", res.msg);
          this.$refs.systemAdd.buttonLoading = false;
        }
      });
    },
    addBusinessSystem() {
      this.$refs.systemAdd.showDialog();
    },
    goBackUrl() {
      this.urlDetailShow = false;
      this.$emit("changeTitle", true);
    },

    moreTool(row, operate) {
      this.itemInfo = row;
      if (operate === "queryInfo") {
        this.getInterface(row, 2);
      }
      if (operate === "viewSensitive") {
        this.handleViewSensitive(row);
      }
      if (operate === "viewDetail") {
        this.handleViewDetail(row);
      } else if (operate === "dataType") {
        this.$emit("changeTitle", false);
        this.showDataType = true;
      } else if (operate === "importData") {
        this.$emit("changeTitle", false);
        this.showImportData = true;
      } else if (operate === "rule") {
        this.$emit("changeTitle", false);
        this.showRuleData = true;
      } else if (operate === "coreData") {
        this.$emit("changeTitle", false);
        this.showCoreData = true;
      }
    },

    handleViewSensitive(row) {
      this.$refs.sensitiveExample.show(row.interfaceSensitiveRuleList || []);
    },
    handleViewDetail(row) {
      this.$emit("changeTitle", false);
      this.urlDetailShow = true;
      this.detailUrl = row.url;
    },
    querySensitive() {
      querySensitiveList().then((res) => {
        if (res && res.code === 200) {
          this.querySensitiveList = res.data.data;
        } else {
          this.querySensitiveList = [];
        }
      });
    },
    queryLevel() {
      queryLevelList().then((res) => {
        if (res && res.code === 200) {
          this.queryLevelList = res.data.data;
        } else {
          this.queryLevelList = [];
        }
      });
    },
    queryTag() {
      queryTagList().then((res) => {
        if (res && res.code === 200) {
          this.queryTagList = res.data.data;
        } else {
          this.queryTagList = [];
        }
      });
    },
    queryType() {
      queryTypeList().then((res) => {
        if (res && res.code === 200) {
          this.queryTypeList = res.data.data;
        } else {
          this.queryTypeList = [];
        }
      });
    },
    getList() {
      this.loading = true;
      this.queryParams.orgId = this.currentNodeKey;

      const params = Object.assign({}, this.queryParams, {
        flagSensitive: this.queryParams.flagSensitive,
        orgId: this.orgId,
        businessSystemId: this.businessSystemId,
        aplManageId: this.appId,
      });
      listInterface(params).then((res) => {
        this.loading = false;
        if (res && res.code === 200) {
          this.interfaceList = res.rows;
          this.interfaceList.forEach((item) => {
            let querySensitive = [];
            let queryTag = [];
            let queryLevel = [];
            let queryType = [];
            let workId = [];
            let levelId = [];
            let typeId = [];
            let tagId = [];
            let workReqId = [];
            let querySensitiveReq = [];
            if (
              item.interfaceApplicationList &&
              item.interfaceApplicationList.length
            ) {
              item.applicationValue =
                item.interfaceApplicationList[0].applicationName;
            } else {
              item.applicationValue = "";
            }

            if (
              item.interfaceDataCoresList &&
              item.interfaceDataCoresList.length
            ) {
              let coreDataName = item.interfaceDataCoresList.reduce((r, c) => {
                if (c.coreName) r.push(c.coreName);
                return r;
              }, []);
              item.coreDataName = coreDataName.length
                ? coreDataName.join(",")
                : "";
            }

            if (
              item.interfaceSensitiveRuleList &&
              item.interfaceSensitiveRuleList.length
            ) {
              item.interfaceSensitiveRuleList.forEach((item) => {
                workId.push(item.workId);
              });
              this.querySensitiveList.forEach((item) => {
                if (workId.includes(item.wordId.toString())) {
                  return querySensitive.push(item.name);
                }
              });
              let ruleDataName = item.interfaceSensitiveRuleList.reduce(
                (r, c) => {
                  if (c.ruleName) r.push(c.ruleName);
                  return r;
                },
                []
              );
              item.ruleDataName = ruleDataName.length
                ? ruleDataName.join(",")
                : "";
            }
            if (
              item.interfaceDataTagsList &&
              item.interfaceDataTagsList.length
            ) {
              item.interfaceDataTagsList.forEach((item) => {
                tagId.push(item.tagId);
              });
              this.queryTagList.forEach((item) => {
                if (tagId.includes(item.tagId.toString())) {
                  return queryTag.push(item.tagName);
                }
              });
              let tagsDataName = item.interfaceDataTagsList.reduce((r, c) => {
                if (c.tagName) r.push(c.tagName);
                return r;
              }, []);
              item.tagsDataName = tagsDataName.length
                ? tagsDataName.join(",")
                : "";
            }
            if (
              item.interfaceDataLevelList &&
              item.interfaceDataLevelList.length
            ) {
              item.interfaceDataLevelList.forEach((item) => {
                levelId.push(item.levelId);
              });
              this.queryLevelList.forEach((item) => {
                if (levelId.includes(item.levelId.toString())) {
                  return queryLevel.push(item.name);
                }
              });

              let levelDataName = item.interfaceDataLevelList.reduce((r, c) => {
                if (c.levelName) r.push(c.levelName);
                return r;
              }, []);
              item.levelDataName = levelDataName.length
                ? levelDataName.join(",")
                : "";
            }
            if (
              item.interfaceDataTypeList &&
              item.interfaceDataTypeList.length
            ) {
              item.interfaceDataTypeList.forEach((item) => {
                typeId.push(item.typeId);
              });
              this.queryTypeList.forEach((item) => {
                if (typeId.includes(item.typeId.toString())) {
                  return queryType.push(item.typeName);
                }
              });
              let dataTypeName = item.interfaceDataTypeList.reduce((r, c) => {
                if (c.typeName) r.push(c.typeName);
                return r;
              }, []);
              item.dataTypeName = dataTypeName.length
                ? dataTypeName.join(",")
                : "";
            }
            if (
              item.interfaceReqSensitiveRuleList &&
              item.interfaceReqSensitiveRuleList.length
            ) {
              item.interfaceReqSensitiveRuleList.forEach((item) => {
                workReqId.push(item.workReqId);
              });
              this.querySensitiveList.forEach((item) => {
                if (workReqId.includes(item.wordId.toString())) {
                  return querySensitiveReq.push(item.name);
                }
              });
            }
            item.querySensitiveReq = querySensitiveReq.join(",");
            // item.querySensitive = querySensitive.join(",");
            item.querySensitive = querySensitive;
            item.queryLevel = queryLevel.join(",");
            item.queryType = queryType.join(",");
            item.queryTag = queryTag.join(",");
          });

          if (this.interfaceList.length > 0) {
            this.interfaceList.map((user) => {
              user.url = user.url;
              return user;
            });
          }
          //this.interfaceList.forEach(item->{item.url= encode(item.url)});
          this.total = res.total;
        } else {
          this.interfaceList = [];
          this.total = 0;
          this.handleListTips(res);
        }
      });
    },

    listSensitive(v) {
      const params = {
        sensitiveName: v,
      };
      listSensitive(params).then((res) => {
        if (res && res.code === 200) {
          this.listSensitArr = res.data;
        } else {
          this.listSensitArr = [];
        }
      });
    },
    matchingFn(id) {
      let name = "";
      this.assetGroupArr && this.assetGroupArr.length
        ? this.assetGroupArr.map((v) => {
            if (v.id === id) {
              name = v.name;
            }
          })
        : null;
      return name;
    },
    /** 敏感标识下拉列表 */
    getSensitiveRule() {
      // const params = {
      //   name : this.assetGroupName,
      //   status: 0,
      //   delFlag: 0,
      // }
      getSensitiveRule().then((res) => {
        let datachildren = [];
        if (res && res.code === 200 && res.data && res.data.length) {
          this.sensitiveRuleArr = res.data;
        } else {
          this.sensitiveRuleArr = [];
        }
      });
    },
    formateFn(arr, key) {
      let newArr = [];
      if (arr && arr.length) {
        arr.map((v) => {
          newArr.push(v[key]);
        });
      }
      return newArr.join(";");
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.assetGroup = data.node.id;
      // this.queryParams.domainId = data.node.id;
      this.getList();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleBatchTrafficAnalysis(item) {
      this.$emit("changeTitle", false);
      this.analysisPath = item.url;
      this.analysisId = item.id;
      this.flowAnalysisDiv = true;
    },
    changeFlowAnalysisDiv() {
      this.flowAnalysisDiv = false;
      this.$emit("changeTitle", true);
    },

    addParamsFn() {
      // 新增参数条数
      this.butOpen = true;
      this.addParamsArr.push(1);
    },
    deleteParamsFn(i) {
      this.addParamsArr.splice(i, 1);
      this.paramType.splice(i, 1);
      this.paramValue.splice(i, 1);
      if (this.addParamsArr && this.addParamsArr.length === 1) {
        this.butOpen = false;
      }
    },
    // 取消按钮
    cancel() {
      this.manualForm.manualType = null;
      this.manualForm.manualTag = null;
      this.open = false;
      this.batchUpdateOpen = false;
      this.detailsOpen = false;
      this.reset();
      this.reParams();
      this.manualOpen = false;
      this.applicationList = [];
    },
    reParams() {
      this.paramType = [];
      this.paramValue = [];
      this.addParamsArr = [1];
    },
    // 表单重置
    reset() {
      this.form = {
        url: null,
        businessSystemName: null,
        businessSystemId: null,
        aplManageId: null,
        applicationValue: null,
        id: null,
        manuCode: null,
        systemCode: null,
        apiName: null,
        domainId: null,
        schema: null,
        domain: null,
        port: null,
        path: null,
        sensitiveList: [],
        method: null,
        auth: 0,
        description: null,
        serverId: null,
        querySensitiveId: null,
        querySensitive: null,
        queryTagId: null,
        queryTag: null,
        currentVisits: 0,
        queryLevelId: null,
        queryLevel: null,
        interfaceSensitiveRuleList: [],
        deptId: null,
        showApplicationName: null,
        passSource: "网关",
        // query: null,
        // apiName: null,
        // fingerprint: null,
        // status: 0,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
      };
      this.resetForm("form");
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      // this.$refs.commonDept.clearDept();
      // this.handleQuery();
    },
    getInterface(row, n) {
      // 1:编辑  2：详情

      const id = row.id || this.ids;
      if (n !== 1) {
        this.infoLoading = true;
        this.detailsOpen = true;
        this.detailsTitle = "详情";
      } else {
        this.infoUpdateLoading = true;
        this.open = true;
        this.title = "编辑API资产台账";
      }
      getInterface({ id }).then((res) => {
        if (res && res.code === 200) {
          let tag = [];
          let tagId = [];
          let sensitiveId = [];
          let sensitive = [];
          res.data.interfaceSensitiveRuleList.forEach((item) => {
            sensitiveId.push(parseInt(item.workId));
          });
          this.querySensitiveList.forEach((item) => {
            if (sensitiveId.includes(item.wordId)) {
              sensitive = sensitiveId;
            }
          });
          res.data.interfaceDataTagsList.forEach((item) => {
            tagId.push(parseInt(item.tagId));
          });
          this.queryTagList.forEach((item) => {
            if (tagId.includes(item.tagId)) {
              tag = tagId;
            }
          });

          this.form = Object.assign(res.data, {
            businessSystemName: res.data.businessName,
            querySensitive:
              res.data.interfaceSensitiveRuleList &&
              res.data.interfaceSensitiveRuleList.length
                ? sensitive
                : [],
            queryLevel:
              res.data.interfaceDataLevelList &&
              res.data.interfaceDataLevelList.length
                ? parseInt(res.data.interfaceDataLevelList[0].levelId)
                : null,
            queryType:
              res.data.interfaceDataTypeList &&
              res.data.interfaceDataTypeList.length
                ? parseInt(res.data.interfaceDataTypeList[0].typeId)
                : null,
            queryTag:
              res.data.interfaceDataTagsList &&
              res.data.interfaceDataTagsList.length
                ? tag
                : [],
            applicationValue:
              res.data.interfaceApplicationList &&
              res.data.interfaceApplicationList.length
                ? row.interfaceApplicationList[0].aplManageId
                : "",
          });

          this.form.showApplicationName =
            res.data.interfaceApplicationList &&
            res.data.interfaceApplicationList.length
              ? row.interfaceApplicationList[0].applicationName
              : "";

          this.isDisabled = res.data.passSource !== "手工添加";
          if (
            res.data &&
            res.data.queryForEditShow &&
            this.isJson(res.data.queryForEditShow)
          ) {
            let paramList = JSON.parse(res.data.queryForEditShow);
            this.addParamsArr = [];
            paramList && paramList.length
              ? paramList.map((v) => {
                  this.addParamsArr.push(1);
                  this.paramType.push(v.paramType);
                  this.paramValue.push(v.paramValue);
                })
              : null;
          }
          if (n !== 1) {
            this.form.queryTag = row.queryTag;
            this.form.queryLevel = row.queryLevel;
            this.form.queryType = row.queryType;
            // this.from.url = row.url;
            this.form.querySensitive =
              row.querySensitive.length === 0
                ? ""
                : row.querySensitive.join(",");

            this.form.applicationValue = row.applicationValue;
            this.form.currentVisits = res.data.currentVisits;
            this.form.querySensitiveReq = row.querySensitiveReq;

            let index = this.parameterFormatData.findIndex(
              (f) => f.value === this.form.contentType
            );
            if (index > -1) {
              this.form.contentTypeName = this.parameterFormatData[index].label;
            }
            this.url = row.url;

            if (
              res.data.interfaceDataCoresList &&
              res.data.interfaceDataCoresList.length
            ) {
              let coreDataName = res.data.interfaceDataCoresList.reduce(
                (r, c) => {
                  if (c.coreName) r.push(c.coreName);
                  return r;
                },
                []
              );
              this.form.coreDataName = coreDataName.length
                ? coreDataName.join(",")
                : "";
            }

            if (
              res.data.interfaceSensitiveRuleList &&
              res.data.interfaceSensitiveRuleList.length
            ) {
              let ruleDataName = res.data.interfaceSensitiveRuleList.reduce(
                (r, c) => {
                  if (c.ruleName) r.push(c.ruleName);
                  return r;
                },
                []
              );
              this.form.ruleDataName = ruleDataName.length
                ? ruleDataName.join(",")
                : "";
            }
            if (
              res.data.interfaceDataTagsList &&
              res.data.interfaceDataTagsList.length
            ) {
              let tagsDataName = res.data.interfaceDataTagsList.reduce(
                (r, c) => {
                  if (c.tagName) r.push(c.tagName);
                  return r;
                },
                []
              );
              this.form.tagsDataName = tagsDataName.length
                ? tagsDataName.join(",")
                : "";
            }
            if (
              res.data.interfaceDataLevelList &&
              res.data.interfaceDataLevelList.length
            ) {
              let levelDataName = res.data.interfaceDataLevelList.reduce(
                (r, c) => {
                  if (c.levelName) r.push(c.levelName);
                  return r;
                },
                []
              );
              this.form.levelDataName = levelDataName.length
                ? levelDataName.join(",")
                : "";
            }
            if (
              res.data.interfaceDataTypeList &&
              res.data.interfaceDataTypeList.length
            ) {
              let dataTypeName = res.data.interfaceDataTypeList.reduce(
                (r, c) => {
                  if (c.typeName) r.push(c.typeName);
                  return r;
                },
                []
              );
              this.form.dataTypeName = dataTypeName.length
                ? dataTypeName.join(",")
                : "";
            }
          }
          this.infoLoading = false;
          this.infoUpdateLoading = false;

          this.form.phone = res.data.phoneNumber
            ? Utils.phoneEncryption(decode(res.data.phoneNumber))
            : "";

          this.form.aplManageId =
            res.data.interfaceApplicationList[0].aplManageId;
          this.form.applicationValue =
            res.data.interfaceApplicationList[0].applicationName;
        } else {
          this.$GlobalErrorConfirm("获取信息失败", res.msg, "重新加载").then(
            () => {
              this.getInterface(row, n);
            }
          );
        }
      });
    },
    isJson(str) {
      if (typeof str == "string") {
        try {
          var obj = JSON.parse(str);
          if (typeof obj == "object" && obj) {
            return true;
          } else {
            return false;
          }
        } catch (e) {
          return false;
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.tree-item-wrap {
  display: flex;
  align-items: center;
  width: 100%;

  &:hover {
    .iconfont {
      opacity: 1;
    }
  }

  .iconfont {
    opacity: 0;
    transition: opacity 0.15s linear;
  }
}

.manualDialog {
  ::v-deep .global-common-edit-dialog-body {
    background-color: #fff;
    padding: 0;
  }

  .manualClass {
    border-bottom: 1px solid #eee;
    margin: 0;

    ::v-deep .el-form-item__label {
      background-color: #f5f5f5;
      padding: 0px 0 7px 0;
    }

    ::v-deep .el-form-item__content {
      padding-left: 8px;
      background-color: #fff;
    }

    ::v-deep .el-input__inner {
      border: #eee;
      padding-left: 0px;
    }

    ::v-deep .el-tag {
      margin: 0 2px 0 0;
    }
  }
}

.paramsList {
  display: flex;
  margin-bottom: 10px;
  align-items: center;

  .delectCurrentParam {
    font-size: 16px;
    margin-left: 10px;
    cursor: pointer;
    position: relative;
    right: 0;
  }

  ::v-deep .el-input-group__prepend {
    background: #fff;
  }
}

.isDisabled {
  ::v-deep .el-input-group__prepend {
    background: #f5f7fa;
  }
}

.tools-switch {
  color: #606266;
  font-size: 14px;
  margin-top: 18px;

  .t-s-label {
    margin-right: 4px;
  }

  .t-s-tips {
    margin-left: 12px;
  }
}

.tree-data-wrap {
  margin-top: 8px;
  max-height: calc(100% - 40px);
  overflow-y: scroll;
}

.icon-piliangfenzu {
  font-size: 12px !important;

  ::v-deep span {
    margin-left: 5px;
  }
}

.el-cascader {
  display: block;
}

::v-deep .application-list {
  .el-cascader-menu {
    .el-cascader-node__label {
      max-width: 190px;
    }
  }
}

.el-dropdown-link {
  color: var(--theme-color);
}
</style>
